package com.qf.web;

import com.qf.pojo.Dept;
import com.qf.service.DeptService;
import com.qf.service.impl.DeptServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

//删除部门信息
@WebServlet("/deleteDept")
public class DeleteDeptServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DeptService deptService=new DeptServiceImpl();
        PrintWriter writer = resp.getWriter();
        String dname=req.getParameter("dname");
        if("".equals(dname)||dname==null){
            writer.println("请输入要删除的部门名称");
        }else {
            //查
            List<Dept> list = deptService.findDeptByDname(dname);
            if(list.size()==1){
                //执行山粗
                int num = deptService.deleteDeptBYDname(dname);
                if(num==1){
                    resp.sendRedirect("/findDeptAll");
                }else {
                    writer.println("部门信息删除失败");
                }
            }else {
                writer.println("您要删除部门信息不存在");
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
